/**
 * 
 * @param 	idField<String> Identifiant du champ date
 * @return	void
 */
function datePickerFriendly( idField )
{
  var field = $("#" +idField);
  field.after('<input type="text" id="alt_' +idField +'" class="datefield-friendly"/>');
  field.datepicker('option','altField','#alt_'+idField);

  var altField = $("#alt_" +idField);
  altField.bind('click', datePickerFriendly_showDatePicker);
  altField.bind('change', datePickerFriendly_updateFormAlt);

  field.css('display', 'none');
}

function dateTimePickerFriendly( idField, altTimeFormat )
{
  
  var field = $("#" +idField);
  field.after('<input type="text" id="alt_' +idField +'" class="datetimefield-friendly"/>');
  
  var altField = $("#alt_" +idField);
  
  field.bind('change', function(){
    var date = field.datetimepicker('getDate');
    altField.val(format_datetime(date, altTimeFormat));
  });
  altField.bind('click', datePickerFriendly_showDatePicker);
  altField.attr('readonly','readonly');
  altField.val(format_datetime(field.datetimepicker('getDate'), altTimeFormat));
  altField.keydown(function(event){
    if( event.keyCode == '8' || event.keyCode == '46' ){
      field.val('');
      altField.val('');
    }
  });
  field.css('display','none');
}

function format_datetime( time, format )
{
  if( !(time instanceof Date) ){
    try{ time = $.datetime.parseDate(format, time.toString()); } 
    catch ( error ){ return ''; }
  }
  
  return $.datepicker.formatDate(format, time)
    .replace(/hh/g, ((time.getHours() < 10) ? '0' : '') + time.getHours())
    .replace(/ii/g, ((time.getMinutes() < 10) ? '0' : '') + time.getMinutes());
}


function dateTimePickerFriendly_updateFormAlt()
{
  /*
  console.info('test');
  console.info($(this).datetimepicker('option','dateFormat'));
 console.debug($(this));//.datetimepicker('timeFormat')); 
  */
}



/**
 * Quand la données du champ altérnatif est modifié manuellement, le champs
 * original est mis à jour, et la donnée éffichée également. Si la date est
 * incorrecte, les champs sont vidées.
 *
 * @author  Jacjsay14
 */
function datePickerFriendly_updateFormAlt()
{
  var opt = datePickerFriendly_getAccess( $(this) );
  try {
    // console.info('###'+$(this).datetimepicker('timeFormat'));
    var newDate = $.datepicker.parseDate(opt.altFormat, opt.altValue);
    opt.dataField.val($.datepicker.formatDate(opt.dataFormat, newDate));
    opt.altField.val($.datepicker.formatDate(opt.altFormat, newDate));
  } catch( err ){
    opt.altField.val('');
    opt.dataField.val('');
  }
}

function datePickerFriendly_showDatePicker()
{
  var opt = datePickerFriendly_getAccess( $(this) );
  opt.dataField.datepicker('show');
}



/**
 * Cette fonction retourne des raccourcis vers les différents champs / données.
 */
function datePickerFriendly_getAccess( altFieldId )
{
  var access = {};
  access.altField = altFieldId;
  access.altValue = altFieldId.val();
  
  var id = access.altField.attr('id').substring(4, access.altField.attr('id').length);
  
  access.dataField  = $('#'+id);
  access.dataFormat = access.dataField.datepicker('option','dateFormat');
  access.dataValue  = $('#'+id).val();

  access.altFormat = access.dataField.datepicker('option','altFormat');
  return access;
}